<template>
  <div class="registerall">
    <h1>登录</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="手机号：" prop="tel">
        <el-input v-model="ruleForm.tel"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码：" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item align="left">
        <el-button type="primary" @click="submitForm" :plain="true"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
    <div class="login-link">
      <el-button type="text" @click="$router.push('/login')"
        >返回登录</el-button
      >
    </div>
  </div>
</template>

<script>
import api from "../../../http/apis/apis";
export default {
  data() {
    var checkTel = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号码不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        tel: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        tel: [{ validator: checkTel, trigger: "blur" }],
      },
    };
  },
  methods: {
    async submitForm() {
      const { data } = await api.customer.register({
        tel: this.ruleForm.tel,
        password: this.ruleForm.pass,
      });
      if (data.status) {
        this.$message({
          message: "注册成功！",
          type: "success",
        });
        this.$router.push("/login");
      } else {
        this.$message.error("注册失败");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.registerall {
  width: 400px;
  margin: 100px auto;
  text-align: center;
  padding: 20px;
  border-radius: 15px;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.08);
  .oinp {
    display: flex;
    align-items: center;
    margin-top: 10px;
    p {
      width: 100px;
    }
    .el-input {
      width: 200px;
    }
  }
}
.registerall:hover {
  transition: 1s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
.inputTxt {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.inputTxt {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-row {
  margin-top: 10px;
  button {
    width: 100px;
  }
}
.login-link {
  margin-top: 10px;
  span {
    color: rgb(144, 176, 215);
  }
}
.el-form-item__content {
  margin-left: 0 !important;
}
</style>